package com.flot.yazb.home.linemoudle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.flot.yazb.R;
import com.flot.yazb.base.BFragment;
import com.flot.yazb.base.adapter.recycleview.BRAdapter;
import com.flot.yazb.base.adapter.recycleview.RVAdapter;
import com.flot.yazb.base.adapter.recycleview.RvHolder;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.video.VideoView;
import com.flot.yazb.model.anchor.AnchorModel;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.widget.utils.GlideImageLoaderUtil;
import com.flot.yazb.widget.utils.ToastUtil;
import com.flot.yazb.widget.MRecycleView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HotFragment extends BFragment {


    @BindView(R.id.mrv)
    MRecycleView mRecycleview;
    private List<AnchorModel> dataList = new ArrayList<>();
    private BRAdapter<AnchorModel> rvAdapter;
    private View view;
    private UserModel argsModel;

    @Override
    protected View initRootView(LayoutInflater inflater) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_home_linehot, null);
        }
        ButterKnife.bind(this, view);
        ViewGroup myparent = (ViewGroup) view.getParent();
        if (getArguments() != null) {
            argsModel = (UserModel) getArguments().getSerializable("args");
        }
        if (myparent != null) {
            myparent.removeView(view);
        }
        return view;
    }

    public static HotFragment newInstance(UserModel model) {
        HotFragment fragment = new HotFragment();
        Bundle args = new Bundle();
        args.putSerializable("args", model);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        initAdapter();
        getData();
    }

    private void getData() {
        showProgress();
        AnchorModel.getAnchorList(new BModel.Transit<AnchorModel[]>(getActivity()) {
            @Override
            public void onBorn(AnchorModel[] serializable, String code, String message) {
                dismissProgress();
                if (code.equals(CodeStub.B_OK)){
                    dataList.clear();
                    dataList.addAll(Arrays.asList(serializable));
                    rvAdapter.notifyDataSetChanged();
                }else {
                    ToastUtil.showToast(getActivity(),message);
                }
            }
        });
    }

    private void initAdapter() {
        mRecycleview.setLayoutManager(new GridLayoutManager(getActivity(),2));
        mRecycleview.setRefreshProgressStyle(ProgressStyle.BallScaleMultiple);//刷新样式
        mRecycleview.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);//加载样式
        mRecycleview.setLoadingMoreEnabled(false);
        mRecycleview.setPullRefreshEnabled(false);
        mRecycleview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //refresh data here
                ToastUtil.showToast(getActivity(), "refresh");
                mRecycleview.refreshComplete();

            }

            @Override
            public void onLoadMore() {
                // load more data here
                mRecycleview.loadMoreComplete();
                ToastUtil.showToast(getActivity(), "onLoadMore");
            }
        });

        rvAdapter=new BRAdapter<AnchorModel>(getActivity(),R.layout.fragment_anchor_item,dataList) {
            @Override
            protected void convert(RvHolder holder, AnchorModel anchorModel, int position) {
                GlideImageLoaderUtil.loadImage((ImageView) holder.getView(R.id.icon),anchorModel.pic);
                GlideImageLoaderUtil.loadImage((ImageView) holder.getView(R.id.thumb),anchorModel.pic);
                holder.setText(R.id.name,anchorModel.name);
                holder.setText(R.id.tvHit,anchorModel.hit+"");
            }
        };

        rvAdapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
//                startActivity(VideoPlayActivity.createIntent(getActivity(),"1"));
                Intent in = new Intent(getActivity(), VideoView.class);
                in.putExtra("anch",dataList.get(position-1));
                startActivity(in);
            }

            @Override
            public void onItemLongClick(View v, int position) {

            }
        });

        mRecycleview.setAdapter(rvAdapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


}
